package com.numberone.master.common.yunzhanghu;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

//云账户的加密类,data加密使用
public class DesBase {

	//加密算法
	public static byte[] TripleDesEncrypt(byte[] content, byte[] key) throws Exception {
	    byte[] icv = new byte[8];
	    System.arraycopy(key, 0, icv, 0, 8);
	    return TripleDesEncrypt(content, key, icv);
	}
	protected static byte[] TripleDesEncrypt(byte[] content, byte[] key, byte[] icv) throws
	        Exception {
	    final SecretKey secretKey = new SecretKeySpec(key, "DESede");
	    final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
	    final IvParameterSpec iv = new IvParameterSpec(icv);
	    cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
	    return cipher.doFinal(content);
	}
	//解密算法
	public static byte[] TripleDesDecrypt(byte[] content, byte[] key) throws Exception {
	    byte[] icv = new byte[8];
	    System.arraycopy(key, 0, icv, 0, 8);
	    return TripleDesDecrypt(content, key, icv);
	}
	protected static byte[] TripleDesDecrypt(byte[] content, byte[] key, byte[] icv) throws
	        Exception {
	    final SecretKey secretKey = new SecretKeySpec(key, "DESede");
	    final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
	    final IvParameterSpec iv = new IvParameterSpec(icv);
	    cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
	    return cipher.doFinal(content);
	}
}
